package com.kang.controller;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.IntemInformation;
import com.kang.service.CategoryService;
import com.kang.service.IntemInformationService;
import com.kang.service.WarehouseService;
import com.kang.until.FileUtil;
import com.vonechina.entity.page.IteminfoPage;

/**
 * 物品信息控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("iteminfo")
@SessionAttributes({ "iteminfoPage" })
public class IteminfoController extends com.kang.controller.Controller {
	/**
	 * 物品信息服务层
	 */
	@Resource
	private IntemInformationService is;
	/**
	 * 物品分类服务层
	 * 
	 */
	@Resource
	private CategoryService cs;
	/**
	 * 仓库服务层
	 */
	@Resource
	private WarehouseService ws;

	/**
	 * 物品信息分页查看
	 * 
	 * @param session
	 * @param model
	 * @param iteminfoPage
	 *            搜索信息封装为实体类
	 * @return
	 */
	@RequestMapping("listItem")
	public String listItem(HttpSession session, Model model, IteminfoPage iteminfoPage) {
		initRight(session);
		if (checkRight("查看物品") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isAdd", checkRight("新增物品") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑物品") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除物品") == SUCCESS);
		model.addAttribute("categorylist", cs.listAllCategory());
		model.addAttribute("list", is.listItem(iteminfoPage));
		iteminfoPage.setPageUrl("iteminfo/listItem.do");
		model.addAttribute("page", iteminfoPage);
		return "basicData/goodsManage/goodsManage";
	}

	/**
	 * 通过物品id查询物品信息ajax接口
	 * 
	 * @param inteminformationid
	 *            物品ID
	 * @return 返回json字符串
	 */
	@RequestMapping("findGoodsById")
	@ResponseBody
	public String findGoodsById(Integer inteminformationid) {
		IntemInformation intemInformation = is.selectByID(inteminformationid);
		return JSON.toJSONString(intemInformation);
	}

	/**
	 * 新增物品分类ajax接口
	 * 
	 * @param session
	 * @param request
	 * @param model
	 * @param goodsImage
	 *            物品图片
	 * @param validityunit
	 *            有效期单位
	 * @param iteminfo
	 *            物品信息自动封装实体类
	 * @return 返回json字符串
	 * @throws Exception
	 */
	@RequestMapping("add")
	@ResponseBody
	public String additeminfo(HttpSession session, HttpServletRequest request, Model model, MultipartFile goodsImage,
			String validityunit, IntemInformation iteminfo) throws Exception {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增物品") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		if (goodsImage.getSize() != 0) {
			String fileName = goodsImage.getOriginalFilename();
			String filePath = fileName.substring(0, fileName.lastIndexOf(".")) + System.currentTimeMillis()
					+ fileName.substring(fileName.lastIndexOf("."));
			goodsImage.transferTo(new File(FileUtil.ITEM_IMAGE_PATH + filePath));
			iteminfo.setPicture(filePath);
		}
		iteminfo.setItemcode(is.generateItemcode());
		iteminfo.setCreatepersonid(userID);
		iteminfo.setLastpersonid(userID);
		iteminfo.setValidity(iteminfo.getValidity() + validityunit);
		int a = is.insertSelective(iteminfo);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 获取物品图片
	 * 
	 * @param session
	 * @param response
	 * @param filename
	 *            物品图片名称
	 */
	@RequestMapping(value = "fileDownload")
	public void download(HttpSession session, HttpServletResponse response, String filename) {
		initRight(session);
		if (checkRight("查看物品") != SUCCESS) {
			return;
		}
		FileUtil.download(response, FileUtil.ITEM_IMAGE_PATH + filename, filename);
	}

	/**
	 * 
	 * @param session
	 * @param request
	 * @param model
	 * @param goodsImage
	 *            物品图片
	 * @param validityunit
	 *            有效期单位
	 * @param iteminfo
	 *            物品信息自动封装实体类
	 * @return 返回json字符串
	 * @throws Exception
	 */
	@RequestMapping("update")
	@ResponseBody
	public String updateiteminfo(HttpSession session, HttpServletRequest request, Model model, MultipartFile goodsImage,
			String validityunit, IntemInformation iteminfo) throws Exception {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑物品") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		if (goodsImage.getSize() != 0) {
			String fileName = goodsImage.getOriginalFilename();
			String filePath = fileName.substring(0, fileName.lastIndexOf(".")) + System.currentTimeMillis()
					+ fileName.substring(fileName.lastIndexOf("."));
			goodsImage.transferTo(new File(FileUtil.ITEM_IMAGE_PATH + filePath));
			iteminfo.setPicture(filePath);
		}
		iteminfo.setLastprocessingtime(new Date());
		iteminfo.setLastpersonid(userID);
		iteminfo.setValidity(iteminfo.getValidity() + validityunit);
		int a = is.updateByPrimaryKeySelective(iteminfo);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 删除物品接口
	 * 
	 * @param session
	 * @param request
	 * @param inteminformationid
	 *            物品ID
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deleteiteminfo(HttpSession session, HttpServletRequest request,
			@RequestParam(value = "inteminformationid", required = false) Integer inteminformationid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除物品") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = is.deleteByPrimaryKey(inteminformationid);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

}